package com.chinaums.sftp.internal.options;


import com.chinaums.sftp.internal.connection.SftpConnectionOptions;


import javax.annotation.Nullable;
import java.util.Optional;

import static org.apache.flink.util.Preconditions.checkNotNull;

public class SftpReadOptions extends SftpConnectionOptions {
    private final String path;
    private final String encoding;
    private final boolean isFirstLineHeader;
    private final String readMode;

    private final boolean deleteRead;
    private final int byteDelimiterLength;


    // FTP_CHECK_OK_FILE
    private final boolean fileCheckOkFile;

    private final boolean isMetric;


    public SftpReadOptions(
            String host,
            Integer port,
            @Nullable String username,
            @Nullable String password,
            int timeout,
            String path,
            String encoding,
            boolean isFirstLineHeader,
            String readMode,
            int byteDelimiterLength,
            boolean fileCheckOkFile,
            boolean deleteRead,
            boolean isMetric
    ) {
        super(host, port, username, password, timeout, path, fileCheckOkFile);
        this.path = path;
        this.encoding = encoding;
        this.isFirstLineHeader = isFirstLineHeader;
        this.readMode = readMode;
        this.deleteRead = deleteRead;
        this.byteDelimiterLength = byteDelimiterLength;
        this.fileCheckOkFile = fileCheckOkFile;
        this.isMetric = isMetric;
    }


    public boolean isMetric() {
        return isMetric;
    }

    @Override
    public boolean isFileCheckOkFile() {
        return fileCheckOkFile;
    }


    @Override
    public String getPath() {
        return path;
    }

    public String getEncoding() {
        return encoding;
    }

    public boolean isFirstLineHeader() {
        return isFirstLineHeader;
    }

    @Override
    public int getTimeout() {
        return timeout;
    }

    public String getReadMode() {
        return readMode;
    }

    public Integer getStreamInterval() {
        return 2000;
    }

    public boolean isDeleteRead() {
        return deleteRead;
    }

    public int getByteDelimiterLength() {
        return byteDelimiterLength;
    }


    public static Builder builder() {
        return new Builder();
    }


    public static class Builder {
        public static final int DEFAULT_TIMEOUT = 10000;

        private String host;
        private Integer port;
        private String username;
        private String password;
        private String path;
        private String encoding;
        private boolean isFirstLineHeader;
        private int timeout;
        private String readMode;
        private boolean deleteRead;
        private int byteDelimiterLength;

        private boolean fileCheckOkFile;

        private  boolean isMetric;

        public SftpReadOptions build() {
            checkNotNull(host, "no host supplied.");
            checkNotNull(username, "no username supplied.");
            checkNotNull(password, "no password supplied.");
            checkNotNull(port, "no port supplied.");
            return new SftpReadOptions(
                    host,
                    port,
                    username,
                    password,
                    timeout,
                    path,
                    encoding,
                    isFirstLineHeader,
                    readMode,
                    byteDelimiterLength,
                    fileCheckOkFile,
                    deleteRead,
                    isMetric
            );
        }

        public Builder setMetric(boolean metric) {
            this.isMetric = metric;
            return this;
        }

        public Builder setFileCheckOkFile(boolean fileCheckOkFile) {
            this.fileCheckOkFile = fileCheckOkFile;
            return this;
        }

        public Builder setHost(String host) {
            this.host = host;
            return this;
        }

        public Builder setPort(Integer port) {
            this.port = port;
            return this;
        }

        public Builder setUsername(String username) {
            this.username = username;
            return this;
        }

        public Builder setPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder setPath(String path) {
            this.path = path;
            return this;
        }

        public Builder setEncoding(String encoding) {
            this.encoding = Optional.of(encoding).orElse("UTF-8");
            return this;
        }

        public Builder setIsFirstLineHeader(Boolean isFirstLineHeader) {
            this.isFirstLineHeader = Optional.of(isFirstLineHeader).orElse(false);
            return this;
        }

        public Builder setTimeout(Integer timeout) {
            this.timeout = Optional.of(timeout).orElse(DEFAULT_TIMEOUT);
            return this;
        }

        public Builder setReadMode(String readMode) {
            this.readMode = readMode;
            return this;
        }

        public Builder setDeleteRead(boolean deleteRead) {
            this.deleteRead = deleteRead;
            return this;
        }

        public Builder setByteDelimiter(int byteDelimiterLength) {
            this.byteDelimiterLength = byteDelimiterLength;
            return this;
        }


    }

}
